﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using bmapi.HelperClass;
using bmapi.Models.DataEntity;
using static bmapi.Models.ShowModel;

namespace bmapi.Controllers
{
    public class DetailController : PageController
    {
        #region 样板详情 
        // GET: api/Detail
        /// <summary>
        /// 样板详情
        /// </summary>
        /// <param name="ModelName"></param>
        [HttpGet, Route("styleDetail")]
        public HttpResponseMessage GetModelDetail(string ModelName)
        {
            string sql = "";
            //获取 图片 服装分类名 客户/品牌 创建时间 这个记录只有一条
            sql = "select id as ModelId,image,type,brand,time from Model_tb,Style_tb where Model_tb.style_id=Style_tb.style_id and Model_tb.name=@name";
            try
            {
                SQLCommand sc = new SQLCommand(sql, "@name", ModelName);
                DataTable dt = sc.ExecuteScalars();
                if(dt==null)
                    return AssistFun.toJson(new { status = 201, tip = "暂时没有样板详细", data = "" });
                List<ModelInfo> modelinfo = AssistFun.DataTableToList<ModelInfo>(dt);
                if (modelinfo.Count > 0)                    return AssistFun.toJson(new { status = 200, tip = "成功", data = new { total = 1, rows = modelinfo } });
                else
                    return AssistFun.toJson(new { status = 201, tip = "暂时没有样板详细", data = "" });
            }
            catch (Exception e)
            {
                return AssistFun.toJson(new { status = 201, tip = e.Message, data = "" });
            }
        }

        /// <summary>
        /// 工序列表 工序名  执行人  时间  
        /// </summary>
        /// <param name="modelid">生产单详情时表示Order表中的ID,款式查询时表示model表中的id</param>
        /// <returns></returns>
        [HttpGet, Route("styleDetailProcess")]
        public HttpResponseMessage GetstyleProcessList(string ModelId)
        {
            string sql = "select Task_tb.code as code,name,User_tb.login_id as loginid,time from Task_tb,User_tb where Task_tb.user_id=User_tb.login_id and Task_tb.id=" + ModelId;
            SQLCommand sc = new SQLCommand(sql);
            try
            {
                int step = -1;
                PC pc = this.GetPC(int.Parse(ModelId));
                if (pc != null)
                    step = pc.step;

                DataTable codeInfo = sc.ExecuteScalars();
                if (codeInfo != null)
                {
                    List<ProcessList> proList = new List<ProcessList>();
                    foreach (DataRow item in codeInfo.Rows)
                    {
                        int code = Convert.ToInt32(item["code"].ToString());
                        ProcessList pro = new ProcessList();
                        pro.Code = code;
                        pro.UserName = item["name"].ToString();
                        pro.CreateTime = Convert.ToDateTime(item["time"]);
                        pro.loginid = Convert.ToInt32(item["loginid"]);
                        if (step == code)
                            pro.State = 1; //未完成
                        else
                            pro.State = 0; //完成

                        proList.Add(pro);
                    }
                    return AssistFun.toJson(new { status = 200, tip = "成功", data = new { total = proList.Count, rows = proList } });
                }
                else
                    return AssistFun.toJson(new { status = 201, tip = "暂时没有信息", data = "" });
            }
            catch (Exception e)
            {
                return AssistFun.toJson(new { status = 201, tip = e.Message, data = "" });
            }

        }
        #endregion

        #region pod详情
        /// <summary>
        /// 订单的id
        /// </summary>
        /// <param name="orderid"></param>
        /// <returns>pod详情 </returns>
        [HttpGet, Route("podinfo")]
        public HttpResponseMessage GetPodInfo(int id)
        {
            string sql = "select pod as Pod,User_tb.name as UserName,time as Time,Pod_tb.state as State from Pod_tb left join User_tb on user_id=login_id where order_id=@orderid order by pod";
            SQLCommand sc = new SQLCommand(sql, "@orderid", id);
            DataTable dt = sc.ExecuteScalars();
            if (dt == null) return AssistFun.toJson(new { status = 201, tip = "没有相关的pod数据" });
            else
            {
                List<ShowPodInfo> podinfo = AssistFun.DataTableToList<ShowPodInfo>(dt);
                return AssistFun.toJson(new { status = 200, tip = "成功", data = new { total = podinfo.Count, rows = podinfo } });
            }

        }

        #endregion

        #region  生产单细节
        //床信息    
        /// <summary>
        /// 
        /// </summary>
        /// <param name="OrderId">生产单号</param>
        /// <returns></returns>
        [HttpGet, Route("bedinfo")]
        public HttpResponseMessage SelectBedInfo(string Id)
        {
            try
            {
                string sql = "select name as BedName,material_name as MaterialCode,lay_len as BedLen,lay_count as LayCount,uc as UC from Bed_tb where order_id=@orderno";
                SQLCommand sc = new SQLCommand(sql, "@orderno", Id);
                DataTable dt = sc.ExecuteScalars();
                List<BedInfo> bedInfo = new List<BedInfo>();
                if (dt != null)
                {
                    bedInfo = AssistFun.DataTableToList<BedInfo>(dt);
                }
                if (bedInfo.Count > 0)
                    return AssistFun.toJson(new { status = 200, tip = "成功", data = new { total = 1, rows = bedInfo } });
                else
                    return AssistFun.toJson(new { status = 201, tip = "没有床信息", data = "" });
            }
            catch (Exception e)
            {
                return AssistFun.toJson(new { status = 201, tip = e.Message, data = "" });
            }
        }
        /// <summary>
        /// 工序列表 工序名  执行人  时间  
        /// </summary>
        /// <param name="modelid">生产单详情时表示Order表中的ID,款式查询时表示model表中的id</param>
        /// <returns></returns>
        [HttpGet, Route("orderDetailProcess")]
        public HttpResponseMessage GetOrderProcessList(string ModelId)
        {
            //string sql = "select Task_tb.code as code,name,User_tb.login_id as loginid,time from Task_tb,User_tb where Task_tb.user_id=User_tb.login_id and Task_tb.id=" + ModelId;
            string sql = "select Task_tb.code as code,name,User_tb.login_id as loginid,time from Task_tb left join User_tb on Task_tb.user_id = User_tb.login_id where Task_tb.id = " + ModelId;
            SQLCommand sc = new SQLCommand(sql);
            try
            {
                int step = -1;
                PC pc = this.GetPC(int.Parse(ModelId));
                if (pc != null)
                    step = pc.step;

                DataTable codeInfo = sc.ExecuteScalars();
                if (codeInfo != null)
                {
                    List<ProcessList> proList = new List<ProcessList>();
                    foreach (DataRow item in codeInfo.Rows)
                    {
                        int code = Convert.ToInt32(item["code"]);
                        ProcessList pro = new ProcessList();
                        pro.Code = code;
                        pro.loginid = item["loginid"] is DBNull ? 0 : Convert.ToInt32(item["loginid"]);
                        pro.UserName = item["name"] is DBNull ? null: item["name"].ToString();
                        pro.CreateTime = Convert.ToDateTime(item["time"]);
                        if (step == code)
                            pro.State = 1;
                        else
                            pro.State = 0;

                        proList.Add(pro);
                    }
                    return AssistFun.toJson(new { status = 200, tip = "成功", data = new { total = proList.Count, rows = proList.OrderBy(p=>p.Code) } });
                }
                else
                    return AssistFun.toJson(new { status = 201, tip = "暂时没有信息", data = "" });
            }
            catch (Exception e)
            {
                return AssistFun.toJson(new { status = 201, tip = e.Message, data = "" });
            }

        }

        /// <summary>
        /// 生产单详细
        /// </summary>
        /// <param name="Id">订单id</param>
        /// <returns></returns>
        [HttpGet, Route("OrderDetail")]
        public HttpResponseMessage OrderDetail(int Id,int ModelId)
        {
            string sql = string.Format("select count(order_id) from LSH_tb where order_id = {0} ", Id);

            SQLCommand sc = null;
            sc = new SQLCommand(sql);
            string clothe = sc.ExecuteScalar();  //人数
            if (clothe == "") clothe = "0";
            sql = string.Format("select remark from order_tb where id = {0}",Id);
            sc = new SQLCommand(sql);
            string remar = sc.ExecuteScalar(); //订单备注
            sql =string.Format("select sex from Style_tb join Model_tb on Style_tb.style_id=Model_tb.style_id where Model_tb.id={0}",ModelId);
            sc = new SQLCommand(sql);
            string sexs = sc.ExecuteScalar(); //性别
            return AssistFun.toJson(new { status = 200, tip = "成功", data = new { clothes = clothe, remark = remar, sex = sexs } });
        }

        #endregion

        #region 产量详情
        [HttpGet, Route("GetProInfo")]
        public HttpResponseMessage GetProInfo(int orderId)
        {
            string sql = "";
            sql = "select type from pl_tb where id=(select o_plid from order_tb where id=" + orderId+") ";
            SQLCommand sc = null;
            sc = new SQLCommand(sql);
            int type=int.Parse(sc.ExecuteScalar());
            //团订
            if (type == 0)
            {
                sql = "select name,lsh,pod from lsh_tb where order_id=" + orderId;
                sc = new SQLCommand(sql);
                DataTable dt = sc.ExecuteScalars();
                if (dt != null)
                {
                    List<ShowProInfo> showProInfo = AssistFun.DataTableToList<ShowProInfo>(dt);
                    return AssistFun.toJson(new RespondModel(200, "成功", new data(1, showProInfo.ConvertAll(c => (object)c))));

                }
                else
                {
                    return AssistFun.toJson(new RespondModel(201, "无数据记录"));
                }
            }
            else
            {
                return AssistFun.toJson(new RespondModel(201, "无数据记录"));
            }
        }
        #endregion
        
    }
}
